/* 
* zxxbox.js 
* © 2010 by zhangxinxu http://www.zhangxinxu.com/
* v1.0 2010-03-20
* v1.1 2010-04-03 #添加拖拽
* v1.2 2010-07-12 #修改浏览器高宽以及页面滚动高度获取
* v2.0 2010-08-01 #重写js，增加可读性，维护性
*                 #添加问答框确认的回调方法
*                 #修复浏览器大小变化时黑背景高度不变化的bug，且弹框一直居中显示
*/
(function($){
	//给页面装载CSS样式
	var css = '<style type="text/css">#zxxBlank{position:absolute; z-index:2000; left:0; top:0; width:100%; height:0; background:black;}.wrap_out{position:absolute; padding:5px; background:#eee; z-index:2000;}.wrap_in{background:#fafafa; border:1px solid #ccc;}.wrap_bar{width:100%; background:#f7f7f7; border-top:3px solid #f9f9f9; border-bottom:3px solid #eeeeee; margin-top:2px;}.wrap_title{line-height:4px; background:#f3f3f3; border-top:4px solid #f5f5f5; border-bottom:5px solid #f1f1f1; margin-top:3px;}.wrap_title span{position:relative; margin-left:10px;}.wrap_body{border-top:1px solid #ddd; background:white;}.wrap_close{color:#34538b; float:right; font-weight:bold; margin-right:10px; margin-top:-15px; font-family:black arial; text-decoration:none;}.wrap_close:hover{text-decoration:none; color:#f30;}.wrap_remind{width:16em; padding:30px 40px 20px;}.wrap_remind p{margin:20px 0 0;}.submit_btn{display:inline-block; padding:3px 12px 1.99px; background:#486aaa; border:1px solid; border-color:#a0b3d6 #34538b #34538b #a0b3d6; color:#f3f3f3; line-height:16px; cursor:pointer; overflow:visible;}.submit_btn:hover{text-decoration:none; color:#ffffff;}.cancel_btn{display:inline-block; padding:3px 12px 1.99px; background:#eee; border:1px solid; border-color:#f0f0f0 #bbb #bbb #f0f0f0; color:#333; line-height:16px; cursor:pointer; overflow:visible;}</style>';
	$("head").append(css);
	$.fn.zxxbox = function(options){
		var that = $(this), defaults = {
			title: "代购岛温馨提示",
			shut: "×",
			bar: true,
			closeable: true,
			fix: false,
			bg: true,
			drag: false,
			index: 2000,
			opacity: 0.5,
			ask: false,
			remind: false,
			asktext: "您确认执行此操作？",
			askcall: function(){
				$.noop();
			},
			remindtext: "您尚未输入提醒的内容。",
			delay: 0,
			closeobject: []
		};
		var s = $.extend({}, defaults, options || {});
		//弹框的显示
		var WRAP = '<div id="zxxBlank" onselectstart="return false;"></div><div class="wrap_out" id="wrapOut"><div class="wrap_in" id="wrapIn"><div id="wrapBar" class="wrap_bar"><div class="wrap_title"><span>'+s.title+'</span></div><a href="javasctipt:void(0);" class="wrap_close" id="wrapClose">'+s.shut+'</a></div><div class="wrap_body" id="wrapBody"></div></div></div>';
		if($("#wrapOut").size()){
			$("#wrapOut").show();
			if(s.bg){
				$("#zxxBlank").show();	
			}else{
				$("#zxxBlank").hide();
			}
		}else{
			$("body").append(WRAP);	
		}
		//一些元素对象，浏览器宽高，滚动高度，页面高度
		var bg = $("#zxxBlank"), out = $("#wrapOut"), bar = $("#wrapBar"), clo = $("#wrapClose"), bd = $("#wrapBody"), w, h, st, ph;
		var f = {
			//装载的内容
			con: function(){
				var main;
				if(s.ask){
					main = $('<div class="wrap_remind">'+s.asktext+'<p><button id="zxxSureBtn" class="submit_btn">确认</button>&nbsp;&nbsp;<button id="zxxCancelBtn" class="cancel_btn">取消</button></p></div>');
				}else if(s.remind){
					main = $('<div class="wrap_remind">'+s.remindtext+'<p><button id="zxxSubmitBtn" class="submit_btn">确认</button</p></div>');
				}else{
					that.show();
					main = that;
				}
				return main;
			},
			wht: function(o){
				//获取任意元素的高宽
				var w_h = {};
				$('<div id="wrapClone" style="position:absolute;left:-6000px;"></div>').appendTo("body").append(o.clone());
				w_h.w = $("#wrapClone").width();
				w_h.h = $("#wrapClone").height();
				$("#wrapClone").remove();
				return w_h;
			},
			//黑背景的宽高透明度等，弹框的位置
			pos: function(){
				w = $(window).width(), h = $(window).height(), st = $(window).scrollTop(), ph = $(document).height();
				bg.width(w).height(ph).css("opacity", s.opacity); 
				//主体内容的位置
				var x_size = f.wht(f.con());
				var xh = x_size.h, xw = x_size.w + 2;
				var t = st + (h - xh)/2, l = (w - xw)/2;
				out.css({
					width: xw,
					top: t,
					left: l,
					zIndex: s.index
				});
			},
			//定位
			posfix: function(){
				if(window.XMLHttpRequest){
					out.css("position","fixed");
				}else{
					$(window).scroll(function(){
						f.pos();						  
					});
				}		
			},
			//居中
			center: function(){
				$(window).resize(function(){
					f.pos();					  
				});
			},
			bgclick: function(){
				bg.click(function(){
					f.hide();
				});
			},
			bghide: function(){
				bg.hide();
			},
			//弹框的隐藏
			hide: function(){
				if(!s.ask && !s.remind){
					f.con().hide().appendTo($("body"));
				}
				out.remove();
				bg.remove();
				return false;
			},
			barhide: function(){
				bar.hide();
			},
			//拖拽
			drag: function(){
				var drag = false;
				var currentX = 0, currentY=0, posX = out.css("left"), posY = out.css("top");
				bar.mousedown(function(e){
					drag = true;
					currentX = e.clientX;
					currentY = e.clientY;							 
				});	
				$(document).mousemove(function(e){
					if(drag){
						var nowX = e.clientX, nowY = e.clientY;
						var disX = nowX - currentX, disY = nowY - currentY;
						out.css("left", parseInt(posX) + disX).css("top", parseInt(posY) + disY);
					}					   
				});
				$(document).mouseup(function(){
					drag = false;
					posX = out.css("left");
					posY = out.css("top");
				});
			},
			//启动
			start: function(){
				bd.append(f.con());
				f.pos();
				f.center();
				if(s.fix){
					f.posfix();
				}
				if(s.drag){
					f.drag();	
				}
				if(!s.bar){
					f.barhide();	
				}
				if(!s.bg){
					f.bghide();
				}
				if(s.closeable){
					f.bgclick();	
				}
				if(s.delay > 0){
					setTimeout(f.hide, s.delay);	
				}
			}
		};
		f.start();
		
		//一些事件的绑定
		$("#zxxSubmitBtn").bind("click", f.hide);
		$("#zxxSureBtn").bind("click",function(){
			s.askcall();
			f.hide();
		});
		$("#zxxCancelBtn").bind("click", f.hide);
		clo.bind("click", f.hide);
		if(s.closeobject.length){
			s.closeobject.each(function(i, n){
				n.bind("click", f.hide);						
			});
		}
	}
})(jQuery);